/*
 * Analyze.cpp
 *
 *  Created on: Oct 4, 2010
 *      Author: gibson
 */

#include "Analyze.h"
#include "DataReader.h"
#include "TH2D.h"
#include "TH1I.h"
#include "TCanvas.h"
#include "TPad.h"
#include "TApplication.h"
#include "globals.hh"
#include "TFile.h"
#include <fstream>

using namespace std;

void TCanvas::Closed() {
	Emit("Closed()");

	gApplication->Terminate(0);
}

Analyze::Analyze(char* path, char* path2) {
	ADCDistribution(path, path2);
}

Analyze::~Analyze() {
	;
}

void Analyze::ADCDistribution(char* path, char* path2) {
	TH2D* ADCMap = new TH2D("asdf", "asdf", 1.8 * m / 2.5 / cm, -.9, .9, 1.8
			* m / 2.5 / cm, -.9, .9);
	readADCMap(ADCMap, path2);
	TH2D* dataHisto = new TH2D("dataHisto", "dataHisto", 72, -0.9, 0.9, 72,
			-0.9, 0.9);
	TH1I* analyzeHisto = new TH1I("adcbd", "ADC Boards per 3P0 Decay",
			200, -0.5, 199.5);
	DataReader* dataReader = new DataReader(path);
	bool proceed = dataReader->ReadNext(dataHisto);

	while (proceed) {
		int ADCCount = 0;
		int* ADCList = new int[ADCCount];

		for (int i = 0; i < 72; i++) {
			for (int j = 0; j < 72; j++) {
				bool reg = false;
				if (i > 11 && j > 11 && i < 60 && j < 60) {
					if (dataHisto->GetBinContent(i + 1, j + 1) > MeV) {
						reg = true;
					}
				} else {
					if ((i + 1) % 2 != 0 && (j + 1) % 2 != 0) {
						int total = dataHisto->GetBinContent(i + 1, j + 1);
						total += dataHisto->GetBinContent(i + 2, j + 1);
						total += dataHisto->GetBinContent(i + 1, j + 2);
						total += dataHisto->GetBinContent(i + 2, j + 2);

						if (total > MeV) {
							reg = true;
						}
					}
				}

				if (reg) {
					int ADCNum = ADCMap->GetBinContent(i + 1, j + 1);
					bool newADC = true;
					for (int k = 0; k < ADCCount; k++)
						if (ADCList[k] == ADCNum)
							newADC = false;
					if (newADC) {
						int* newADCList = new int[ADCCount + 1];
						for (int k = 0; k < ADCCount; k++)
							newADCList[k] = ADCList[k];
						newADCList[ADCCount] = ADCNum;
						delete ADCList;
						ADCList = newADCList;
						ADCCount++;
					}
				}
			}
		}

		analyzeHisto->Fill(ADCCount);
		delete dataHisto;
		dataHisto = new TH2D("dataHisto", "dataHisto", 72, -0.9, 0.9, 72, -0.9,
				0.9);
		proceed = dataReader->ReadNext(dataHisto);
		delete ADCList;
	}

	delete dataHisto;
	delete dataReader;

	TCanvas* canvas = new TCanvas("canvas",
			"Number of Crystals Hit per Klong Event", 500, 400);
	analyzeHisto->Draw();

	TFile* file = new TFile("./output.root", "update");
	analyzeHisto->Write();
	file->Close();

	gApplication->Run(true);

	delete analyzeHisto;
}

void Analyze::readADCMap(TH2D* ADCMap, char* path2) {
	ifstream file;
	file.open(path2);

	char* number = new char[1024];
	for (int j = 0; j < 1024; j++)
		number[j] = ' ';
	int numberCount = 0;

	bool ADCFlag = true;
	int ADCNumber = 0;

	while (file.good()) {
		char c = file.get();
		if (c == '\t') {
			if (ADCFlag) {
				//				G4cout << "ADC number: " << atoi(number) << "\tCrystals: ";
				ADCNumber = atoi(number);
				ADCFlag = false;
			} else {
				//				G4cout << atoi(number) << " ";
				int num = atoi(number);
				int i;
				int j;
				if (num < 3000) {
					j = num % 48 + 12;
					i = (num - j + 12) / 48 + 12;
					ADCMap->SetBinContent(i + 1, j + 1, ADCNumber);
				} else {
					j = (num - 3000) % 36;
					i = (num - 3000 - j) / 36;
					ADCMap->SetBinContent(2 * i + 1, 2 * j + 1, ADCNumber);
					ADCMap->SetBinContent(2 * i + 2, 2 * j + 1, ADCNumber);
					ADCMap->SetBinContent(2 * i + 1, 2 * j + 2, ADCNumber);
					ADCMap->SetBinContent(2 * i + 2, 2 * j + 2, ADCNumber);
				}
			}
			numberCount = 0;
			for (int j = 0; j < 1024; j++)
				number[j] = ' ';
			continue;
		}
		if (c == '\n' || c == EOF) {
			//			G4cout << atoi(number) << G4endl;
			if (!ADCFlag) {
				int num = atoi(number);
				int i;
				int j;
				if (num < 3000) {
					j = num % 48 + 12;
					i = (num - j + 12) / 48 + 12;
					ADCMap->SetBinContent(i + 1, j + 1, ADCNumber);
				} else {
					j = (num - 3000) % 36;
					i = (num - 3000 - j) / 36;
					ADCMap->SetBinContent(2 * i + 1, 2 * j + 1, ADCNumber);
					ADCMap->SetBinContent(2 * i + 2, 2 * j + 1, ADCNumber);
					ADCMap->SetBinContent(2 * i + 1, 2 * j + 2, ADCNumber);
					ADCMap->SetBinContent(2 * i + 2, 2 * j + 2, ADCNumber);
				}
			}
			ADCFlag = true;
			numberCount = 0;
			for (int j = 0; j < 1024; j++)
				number[j] = ' ';
			continue;
		}

		number[numberCount] = c;
		numberCount++;
	}

	delete number;
}
